﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Common.UI;
using System.IO;

namespace CBE.UI.Shell
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {

        private void Application_Startup(object sender, StartupEventArgs e)
        {

            this.ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose;
            
            Shell shell = new Shell();

            this.MainWindow = shell;

            //login
            Login.ShowLogin();

            if (SEG.Session.Usuario == null)
            {
                this.Shutdown();
            }
            else
            {
                //si login ok.. shell
                MainView.View = shell;

                shell.Show();
            }
        }

        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            this.ShowMessageException(e.Exception);
            this.LogException(e.Exception);
            e.Handled = true;
            //this.Shutdown();
        }

        private void ShowMessageException(Exception exception)
        {
            MessageBox.Show("Ha ocurrido un error en la aplicación. Por favor, intente nuevamente; si el error persiste, contacte al administrador del sistema con el log de error.");
        }

        private void LogException(Exception exception)
        {
            StreamWriter sw = null;

            try
            {
                sw = new StreamWriter("ErrorDelSistema.txt", true);

                sw.WriteLine();
                sw.WriteLine("*********************************************************");
                sw.WriteLine(String.Format("Error ocurrido fecha: {0}", DateTime.Now.ToString()));
                sw.WriteLine("*********************************************************");
                sw.WriteLine("--------------");
                sw.WriteLine("Message: ");
                sw.WriteLine("--------------");
                sw.WriteLine(exception.Message);

                sw.WriteLine("--------------");
                sw.WriteLine("Exception: ");
                sw.WriteLine("--------------");
                sw.WriteLine(exception.ToString());
                if (exception.InnerException != null)
                {
                    sw.WriteLine("--------------");
                    sw.WriteLine("InnerException: ");
                    sw.WriteLine("--------------");
                    sw.WriteLine(exception.InnerException.ToString());
                }
                sw.WriteLine("--------------");
                sw.WriteLine("Stack trace: ");
                sw.WriteLine("--------------");
                sw.WriteLine(exception.StackTrace.ToString());

            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
            }
        }

    }
}
